/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package extension.game.poker;

import java.util.LinkedList;
import java.util.List;
import log.Debug;

/**
 *
 * @author daz
 */
public class PokerFinisher {

    private List<PokerPlayer> listWinners;
    private int numberFinishPlayer;
    private PokerPlayer[] listPlayerRemain;

    public PokerFinisher() {
        listWinners = new LinkedList<PokerPlayer>();

    }

    public void generateWinner(PokerPlayer[] listPlayers) {
        int arrSize = listPlayers.length;
        Debug.d("generate winner :" + arrSize);
        this.numberFinishPlayer = arrSize;
        listPlayerRemain = new PokerPlayer[arrSize];
        for (int i = 0; i < arrSize - 1; i++) {
            for (int j = i + 1; j < arrSize; j++) {
                if (listPlayers[j].handRank > listPlayers[i].handRank) {
                    PokerPlayer pokerPlayer = listPlayers[i];
                    listPlayers[i] = listPlayers[j];
                    listPlayers[j] = pokerPlayer;
                }
            }
        }
        for (int i = 0; i < arrSize; i++) {
            Debug.d("add to remain :" + listPlayers[i].getName() + ", rank :" + listPlayers[i].handRank);
            listPlayerRemain[i] = listPlayers[i];
        }
//        System.arraycopy(listPlayers, 0, listPlayerRemain, 0, arrSize);

    }

    public List<PokerPlayer> getListWinners(int startIndex) {
        listWinners.clear();
        for (int i = startIndex; i < numberFinishPlayer; i++) {
            if (listPlayerRemain[i].handRank == listPlayerRemain[startIndex].handRank) {
                Debug.d("get list winner :" + listPlayerRemain[i].getName());
                listWinners.add(listPlayerRemain[i]);
            }
        }
        return listWinners;
    }
}
